home *** CD-ROM | disk | FTP | other *** search
/ PsL Monthly 1993 December / PSL Monthly Shareware CD-ROM (December 1993).iso / prgmming / dos / pascal / fielddh.exe / STR_STF.PAS < prev   
Pascal/Delphi Source File  |  1992-04-22  |  19KB  |  512 lines

  1. UNIT STR_STF;
  2.   {**    STRING Definition and OPERATIONS  ***}
  3.  
  4. {$O-,F+,D-}
  5.  
  6. INTERFACE
  7. {**************************************************************}
  8. {* Trim   removes leading/trailing blanks.                    *}
  9. {*                                                            *}
  10. {**************************************************************}
  11. FUNCTION TRIM        (Str : string) : string;
  12.  
  13. FUNCTION TRIM_Leading_Only (Str : string) : string;
  14. FUNCTION TRIM_Trailing_Only (Str : string) : string;
  15. FUNCTION TRIM_Quotes (Str : string) : string;
  16.  
  17. {**************************************************************}
  18. {* Right_Justify adds leading blanks.                         *}
  19. {*    NOTE: does not handle cases when                        *}
  20. {*                   Size_To_Be < ACTUAL NUMBER OF CHARACTERS *}
  21. {**************************************************************}
  22. FUNCTION Right_Justify (Str : string; Size_To_Be : integer) : string;
  23.  
  24. {***************************************************************}
  25. {* Center_Str   centers the characters in the string based     *}
  26. {*              upon the size/midpoint specified.              *}
  27. {***************************************************************}
  28. FUNCTION Center_Str (Str : string; Output_Size : integer) : string;
  29.  
  30. {**************************************************************}
  31. {* Change_Case changes the case of the string to UPPER.       *}
  32. {*                                                            *}
  33. {**************************************************************}
  34. FUNCTION CHANGE_CASE (Str : string) : string;
  35. FUNCTION Lower_Case (Str : string) : string;
  36.  
  37. {**************************************************************}
  38. {* Int_To_Str returns the number converted into ascii chars.  *}
  39. {*                                                            *}
  40. {**************************************************************}
  41. FUNCTION Int_To_Str  (Num : LongInt) : string;
  42.  
  43.  
  44. {**************************************************************}
  45. {* Find_Char   returns the position of the char               *}
  46. {*                                                            *}
  47. {**************************************************************}
  48. FUNCTION Find_Char   (Str      : string;
  49.                       Char_Is  : char;
  50.                       Start_At : integer) : INTEGER;
  51.  
  52. {**************************************************************}
  53. {* Delete_The_Char   delete all occurances of the char        *}
  54. {*                                                            *}
  55. {**************************************************************}
  56. FUNCTION Delete_The_Char
  57.                      (Str      : string;
  58.                       Char_Is  : char) : string;
  59.  
  60. {**************************************************************}
  61. {* Replace_Str_Into  inserts the small string into the        *}
  62. {*                   org_str at the position specified        *}
  63. {**************************************************************}
  64. FUNCTION Replace_Str_Into (Org_Str     : String;
  65.                            Small_Str   : string;
  66.                            Start, Stop : integer) : string;
  67.  
  68. {**************************************************************}
  69. {* procedure Get_Word_Around_Position                         *}
  70. {*     returns the word based AROUND the position specified   *}
  71. {*     Searches for blanks around the start_pos               *}
  72. {*        looking left then right.                            *}
  73. {**************************************************************}
  74. function Get_Word_Around_Position
  75.                      (Str                    : string;
  76.                       Start_Pos              : integer;
  77.                       Leftmost_Char_Boundry  : integer;
  78.                       Rightmost_Char_Boundry : integer;
  79.                       VAR Found_Left_Pos     : integer;
  80.                       VAR Found_Word_Size    : integer) : string;
  81.  
  82. {**************************************************************}
  83. {* returns a string with duplicate chars deleted.             *}
  84. {**************************************************************}
  85. function Delete_Duplicate_Chars_In_Str (Str            : string;
  86.                                         Limit_In_A_Row : byte): string;
  87.  
  88. {**************************************************************}
  89. {* returns a string filled with the character specified       *}
  90. {**************************************************************}
  91. function Fill_String(Len : Byte; Ch : Char) : String;
  92.  
  93. {**************************************************************}
  94. {* Truncates a string to a specified length                   *}
  95. {**************************************************************}
  96. function Trunc_Str(TString : String; Len : Byte) : String;
  97.  
  98. {**************************************************************}
  99. {* Pads a string to a specified length with a specified character }
  100. {**************************************************************}
  101. function Pad_Char(PString : String; Ch : Char; Len : Byte) : String;
  102.  
  103.  
  104. {**************************************************************}
  105. {* Left-justify a string within a certain width               *}
  106. {**************************************************************}
  107. function Left_Justify_Str (S : String; Width : Byte) : String;
  108.  
  109. {**************************************************************}
  110. {**************************************************************}
  111. {**************************************************************}
  112. IMPLEMENTATION
  113.  
  114. {**************************************************************************}
  115. function Min(N1, N2 : Longint) : Longint;
  116. { Returns the smaller of two numbers }
  117. begin
  118.   if N1 <= N2 then
  119.     Min := N1
  120.   else
  121.     Min := N2;
  122. end; { Min }
  123.  
  124. {**************************************************************************}
  125. function Max(N1, N2 : Longint) : Longint;
  126. { Returns the larger of two numbers }
  127. begin
  128.   if N1 >= N2 then
  129.     Max := N1
  130.   else
  131.     Max := N2;
  132. end; { Max }
  133.  
  134. {**************************************************************}
  135. {* returns a string filled with the character specified       *}
  136. {**************************************************************}
  137. function Fill_String(Len : Byte; Ch : Char) : String;
  138. var
  139.   S : String;
  140. begin
  141.   IF (Len > 0) THEN
  142.     BEGIN
  143.       S[0] := Chr(Len);
  144.       FillChar(S[1], Len, Ch);
  145.       Fill_String := S;
  146.     END
  147.   ELSE Fill_String := '';
  148. end; { FillString }
  149.  
  150. {**************************************************************}
  151. {* Truncates a string to a specified length                   *}
  152. {**************************************************************}
  153. function Trunc_Str(TString : String; Len : Byte) : String;
  154. begin
  155.   if (Length(TString) > Len) then
  156.     begin
  157.       {Delete(TString, Succ(Len), Length(TString) - Len);}
  158.       {Move(TString[Succ(Len)+(LENGTH(TString)-Len)], TString[Succ(Len)],
  159.            Succ(Length(TString)) - Succ(Len) - Length(TString) - Len));}
  160.       Move(TString[LENGTH(TString)+1], TString[Succ(Len)], 2*Len);
  161.       Dec(TString[0], Length(TString) - Len);
  162.     end;
  163.   Str_Stf.Trunc_Str := TString;
  164. end; { TruncStr }
  165.  
  166. {**************************************************************}
  167. {* Pads a string to a specified length with a specified character }
  168. {**************************************************************}
  169. function Pad_Char(PString : String; Ch : Char; Len : Byte) : String;
  170. var
  171.   CurrLen : Byte;
  172. begin
  173.   CurrLen := Min(Length(PString), Len);
  174.   PString[0] := Chr(Len);
  175.   FillChar(PString[Succ(CurrLen)], Len - CurrLen, Ch);
  176.   Pad_Char := PString;
  177. end; { PadChar }
  178.  
  179. {**************************************************************}
  180. {* Left-justify a string within a certain width               *}
  181. {**************************************************************}
  182. function Left_Justify_Str(S : String; Width : Byte) : String;
  183. begin
  184.   Left_Justify_Str := Str_Stf.Pad_Char(S, ' ', Width);
  185. end; { Left_Justify_Str }
  186.  
  187. {**************************************************************}
  188. {* Trim   removes leading/trailing blanks.                    *}
  189. {*